package com.str.set;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class SetTest {
	
  private static Set createSet(int i) {
  	if (i == 1) return new ArraySet();
    if (i == 2) return new LinkedSet();
  	return null;
  }
    
  public static void main(String[] args) throws IOException {
    BufferedReader stdin = new BufferedReader(
    	                   new InputStreamReader(System.in)); 	
  	   
    while (true) {
      System.out.println("1. Create Array Set");
      System.out.println("2. Create Linked Set");    	         
      System.out.print("\tEnter your choice(Enter to quit): ");          
      
      String inputLine = stdin.readLine();
      if (inputLine.equals("")) {
        System.out.println("End of run.");
        System.exit(0);
      }     
      
      int choice = Integer.parseInt(inputLine);
      Set thisSet = createSet(choice);
      if (thisSet == null) {   	
        System.out.println("Invalid choice.");
        continue;
      }     
      Set thatSet = null;
      
      while (true) {
        System.out.println("-------------------------");
        System.out.println("     1. Clear");      
        System.out.println("     2. Add");    
        System.out.println("     3. Remove");
        System.out.println("     4. Contains");
        System.out.println("     5. Clone");
        System.out.println("     6. Union");      
        System.out.println("     7. Intersection");    
        System.out.println("     8. Difference");
        System.out.println("     9. Iquals");
        System.out.println("    10. Is subset");
        System.out.print("\tEnter your choice(Enter to quit): ");
      
        inputLine = stdin.readLine();      
        if (inputLine.equals("")) break;
        choice = Integer.parseInt(inputLine);

        switch (choice) {
          case 1:
            System.out.println("Cleared.");
            thisSet.clear();
            break;

          case 2:
            System.out.print("Enter a string: ");
            Object val = stdin.readLine();
            thisSet.add(val);
            break;

          case 3:
            System.out.print("Enter a string: ");
            val = stdin.readLine();
            if (thisSet.contains(val)) {
              thisSet.remove(val);
              System.out.println(val + " has been deleted.");
            }
            else
              System.out.println("No found " + val);
            break;

          case 4:
            System.out.print("Enter a string: ");
            val = stdin.readLine();
            if (thisSet.contains(val))
              System.out.println("Found " + val);
            else
              System.out.println("No found " + val);
            break;

          case 5:
           // thatSet = (Set) thisSet.clone();
            break;
            
          case 6:
            System.out.println("UInion:");
            if (thatSet != null)
              thisSet.union(thatSet);
            break;
          
          case 7:
            System.out.println("Intersection:");
            if (thatSet != null)
              thisSet.intersection(thatSet);
            break;
   
          case 8:
            System.out.println("Difference:");
            if (thatSet != null)
              thisSet.difference(thatSet);
            break;
          
          case 9:
            if (thatSet != null)
              if (thisSet.equals(thatSet)) 
                System.out.println("This set is equal to that.");
              else            
                System.out.println("This set is not equal to that.");
            break;
          
          case 10:
            if (thatSet != null)
              if (thisSet.isSubset(thatSet)) 
                System.out.println("This set is a subset of that.");
              else            
                
                System.out.println("This set is not a subset of that.");
            break;
            
          default:
            System.out.println("Invalid choice.");
            continue;
        }
      
        System.out.println("This set:\n" + thisSet);
        System.out.println("Size: " + thisSet.size());
        if (thatSet != null) {
          System.out.println("That set:\n" + thatSet);
          System.out.println("Size: " + thatSet.size());
        }
      }
    }
  }
}
